home *** CD-ROM | disk | FTP | other *** search
/ bioinformatics.org / bioinformatics.org_software.tar / www.bioinformatics.org / download / ecell2 / ecell220setup.exe / {app} / standard / SRCJ / TracerDataManager.java < prev   
Text File  |  2002-05-13  |  7KB  |  303 lines

  1. /**
  2.  * title:     Tracer Data Manager class (TracerDataManager.java)<p>
  3.  * description  : Save/restor the running data on Tracer<p>
  4.  * Copyright (C) 1996-2001 Keio University <p>
  5.  * Copyright (C) 1998-2001 Japan Science and Technology Corporation (JST)<p>
  6.  *               GNU General Public Licence <p>
  7.  * Division:     Mitui Knowledge Industry Co. Ltd. Bioscience division <p>
  8.  * Version :     $Id: TracerDataManager.java,v 1.6 2002/05/13 00:23:04 ota Exp $ <p>
  9.  */
  10.  
  11. package ecell;
  12.  
  13. import java.util.*;
  14. import java.io.*;
  15.  
  16.  
  17. /**
  18.  * Class for manage the running data on Tracer
  19.  * @author      akira shiozawa
  20.  * @version     1.0
  21.  * @since       JDK1.2.2
  22.  */
  23. class TracerDataManager
  24. {
  25.     /**
  26.      * file extention for tracer data
  27.      */
  28.     public static final String szFileExt = ".tdm";
  29.     
  30.     /**
  31.      * hashtable for RandomAccessFile object
  32.      */
  33.     private /*volatile*/ Hashtable hashType = new Hashtable();
  34.  
  35.     /**
  36.      * file write access permission (do NOT access to file if it's true)
  37.      */
  38.     private boolean bDel = false;
  39.  
  40.     /**
  41.      * directory path for tdm files
  42.      */
  43.     public static String szFilePath = "./tdm";
  44.  
  45.  
  46.  
  47.     public TracerDataManager()
  48.     {
  49.         TracerDataManager.createTempDir();
  50.     }
  51.  
  52.     /**
  53.      * append tracer-data to .tdm(Tracer Data Management) files 
  54.      * @param type      data type
  55.      * @param value     data to save
  56.      * @return boolean
  57.      */
  58.     boolean appendTracerData( String id, double[] value ) throws IOException
  59.     {
  60.         Object fl = null;
  61.         boolean ret = false;
  62.         
  63.         if( bDel )
  64.         {
  65.             return ret;
  66.         }
  67.         
  68.         try
  69.         {
  70.             String fname = id + TracerDataManager.szFileExt;
  71.             if( hashType.get( fname ) == null )
  72.             {
  73.                 hashType.put( fname, "" );
  74.                 File destFile = new File( szFilePath + "/" + fname );
  75. //System.out.println( "destFile:" + fname );
  76.                 if( destFile.exists() )
  77.                 {
  78. //System.out.println( "delete destination file:" + fname );
  79.                     destFile.delete();
  80.                 }
  81.                 destFile = null;
  82.             }
  83.             fl = new RandomAccessFile( szFilePath + "/" + fname, "rw" );
  84. /**
  85.             // prepare destination file
  86.             fl = hashType.get( fname );
  87.             if( fl == null || fl instanceof String )
  88.             {
  89.                 fl = new RandomAccessFile( fname, "rw" );
  90.                 hashType.put( fname, ((RandomAccessFile)fl) );
  91.             }
  92. **/
  93.             ((RandomAccessFile)fl).seek( ((RandomAccessFile)fl).length() );
  94.             
  95.             // write data into destination file
  96.             for( int i = 0; i < value.length; i++ )
  97.             {
  98.                 ((RandomAccessFile)fl).writeDouble( value[i] );
  99.             }
  100.             
  101.             ((RandomAccessFile)fl).close();
  102.             
  103.             ret = true;
  104.         }
  105.         catch( FileNotFoundException e )
  106.         {
  107.             e.printStackTrace();
  108.         }
  109.         catch( IOException e )
  110.         {
  111.             ((RandomAccessFile)fl).close();
  112.             throw e;
  113.         }
  114.         catch( Exception e )
  115.         {
  116.             e.printStackTrace();
  117.         }
  118.         
  119.         
  120.         return ret;
  121.     }
  122.  
  123.  
  124.     /**
  125.      * read tracer-data by block
  126.      * @param type      data type
  127.      * @param value     buffer for read data
  128.      * @param starindex start posotion for read (0-)
  129.      * @param maxsize   size of data block to read at a time
  130.      * @return int      read data size, EOF when -1
  131.      */
  132.     int getTracerDataBlock( String id, double[][] value, int startindex, int maxsize )
  133.     {
  134.         Object fl = null;
  135.         int ret = -1;
  136.         int size = value[0].length;
  137.         byte[] buf = new byte[8 * size * maxsize];
  138.         
  139.         try
  140.         {
  141.             String fname = id + TracerDataManager.szFileExt;
  142.             fl = hashType.get( fname );
  143.             if( fl == null || fl instanceof String )
  144.             {
  145.                 fl = new RandomAccessFile( szFilePath + "/" + fname, "rw" );
  146.                 hashType.put( fname, (RandomAccessFile)fl );
  147. //                throw new FileNotFoundException( "file not found :" + fname );
  148.             }
  149.  
  150.             //((RandomAccessFile)fl).seek( 8 * size * startindex );
  151.             int bufcount = ((RandomAccessFile)fl).read( buf );
  152. //System.out.println( "bufcount:" + bufcount );
  153. //System.out.println( "offset:" + (8 * size * startindex) );
  154.             int count = 0;
  155.             for( count = 0; count < bufcount / 8; count++ )
  156.             {
  157.                 long data = 0L;
  158.                 for( int i = 0; i < 8; i++ )
  159.                 {
  160.                     data = (data << 8) | (buf[i + count * 8] & 0xFF);
  161.                 }
  162. //System.out.println( "value[" + (count / size) + "][" + (count % size) + "]" );
  163.                 value[count / size][count % size] = Double.longBitsToDouble( data );
  164.             }
  165.             ret = (count == 0)? -1 : count / size;
  166.             if( ret < 0 &&  fl != null )
  167.             {
  168. //System.out.println( "---------> " + fname + " closed" );
  169.                 ((RandomAccessFile)fl).close();
  170.                 hashType.put( fname, "" );
  171.                 fl = null;
  172.             }
  173. //System.out.println( "count:" + count / size );
  174.  
  175.         }
  176.         catch( FileNotFoundException e )
  177.         {
  178.             //e.printStackTrace();
  179.         }
  180.         catch( IOException e )
  181.         {
  182.             e.printStackTrace();
  183.         }
  184.         catch( Exception e )
  185.         {
  186.             e.printStackTrace();
  187.         }
  188.  
  189.         return ret;
  190.     }
  191.     
  192.     
  193.     /**
  194.      * delete .tdm files (you must call this method before finishing the program)
  195.      * @return void
  196.      */
  197.     public void deleteTempFiles()
  198.     {
  199.         Object fl = null;
  200.         String fname = null;
  201.         File f = null;
  202.         
  203.         bDel = true;
  204. //System.out.println( "hashType.size():" + hashType.size() );
  205.         for( Enumeration enum = hashType.keys(); enum.hasMoreElements(); )
  206.         {
  207.             fname = (String)enum.nextElement();
  208. //System.out.println( "fname:" + fname );
  209.             fl = hashType.get( fname );
  210.             try
  211.             {
  212.                 if( fl != null && fl instanceof RandomAccessFile )
  213.                 {
  214. //System.out.println( "close file:" + fname );
  215.                     ((RandomAccessFile)fl).close();
  216.                 }
  217.             }
  218.             catch( Exception e ){ e.printStackTrace(); }
  219.             f = new File( TracerDataManager.szFilePath + File.separator + fname );
  220.             f.delete();
  221.             f = null;
  222.         }
  223.     }
  224.     
  225.     
  226.     public static void clearTempDir()
  227.     {
  228.         ExtensionFilter filter = new ExtensionFilter( 
  229.                                     TracerDataManager.szFilePath, TracerDataManager.szFileExt );
  230.         String[] list = filter.list();
  231.  
  232. //System.out.println( "-----------------------------------------------cleartempdir:" + list );
  233.         if( list != null )
  234.         {
  235.             for( int i = 0; i < list.length; i++ )
  236.             {
  237.                 File f = new File( TracerDataManager.szFilePath + File.separator + list[i] );
  238. //System.out.println( "fname:" + list[i] );
  239.                 if( f.exists() )
  240.                 {
  241. //System.out.println( "deleted " + list[i] );
  242.                     f.delete();
  243.                 }
  244.             }
  245.         }
  246.     }
  247.  
  248.     public static void setTempDir( String path )
  249.     {
  250.         if( path != null && !path.trim().equals( "" ) )
  251.         {
  252.             TracerDataManager.szFilePath = path;
  253.         }
  254.     }
  255.  
  256.     public static void createTempDir()
  257.     {
  258.         File filedir = new File( TracerDataManager.szFilePath );
  259.         if( !filedir.exists() )
  260.         {
  261.             try
  262.             {
  263.                 filedir.mkdir();
  264.                 filedir.deleteOnExit();
  265.             }
  266.             catch( Exception e )
  267.             {
  268.                 e.printStackTrace();
  269.             }
  270.         }
  271.     }
  272.  
  273. /**
  274.     public static void main( String[] args )
  275.     {
  276.         TracerDataManager cls = new TracerDataManager();
  277.         double[][] value = {{1.1,2.2,3.3,4.4},{5.5,6.6,7.7,8.8}};
  278.         
  279.         for( int i = 0; i < value.length; i++ )
  280.         {
  281.             cls.appendTracerData( 1, value[i] );
  282.         }
  283.         
  284.         double result[][] = new double[100][4];
  285.         int count = cls.getTracerDataBlock( 1, result, 0, 100 );
  286.         System.out.println( "--------> count : " + count );
  287.         for( int i = 0; i < count; i++ )
  288.         {
  289.             for( int j = 0; j < result[i].length; j++ )
  290.             {
  291.                 System.out.println( "value:" + result[i][j] );
  292.             }
  293.             System.out.println( "-----------------------" );
  294.         }
  295.         count = cls.getTracerDataBlock( 1, result, 100, 100 );
  296.         System.out.println( "end:" + count );
  297.         //cls.deleteTempFiles();
  298.     }
  299. **/
  300.  
  301. }
  302.  
  303.